package com.example.administrator.x3picturetransliteration.Home.Presenter.Activity;

import android.content.Context;

import com.example.administrator.x3picturetransliteration.Base.BasePresenter;
import com.example.administrator.x3picturetransliteration.Base.HttpGo;
import com.example.administrator.x3picturetransliteration.Bean.HttpDataBean;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import rx.subscriptions.CompositeSubscription;

/**
 * 识别结果
 * Created by Administrator on 2019/9/4.
 */

public class RecognitionResultsPresenter extends BasePresenter{

    public RecognitionResultsPresenter(Context context, CompositeSubscription mCompositeSubscription) {
        super(context, mCompositeSubscription);
    }

    /** 上传图片识别 */
    public void document(List<String> url, final Callback mCallback){

        final List<File> httpdata = new ArrayList<>();

        for (String path:url)httpdata.add(new File(path));

        mCompositeSubscription.add(

                mHttpGo.fileSubscription("xfyun/recognize_document", httpdata, new HttpGo.HttpGoCallback() {
                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onNext(HttpDataBean httpDataBean) {

                        if (httpDataBean.getCode()==0){

                            if (httpDataBean.getData()!=null)mCallback.getData(httpDataBean.getData());
                        }
                    }
                })
        );

    }

    /** 表格识别 */
    public void tableParse(int ocr,final String url, final Callback mCallback){

        mCompositeSubscription.add(

                mHttpGo.fileSubscription(ocr==0?"xfyun/ocrTable":"xfyun/ocrWord", new File(url), new HttpGo.HttpGoCallback() {
                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onNext(HttpDataBean httpDataBean) {

                        if (httpDataBean.getCode()==0){

                            if (httpDataBean.getData()!=null)mCallback.getData(httpDataBean.getData());
                        }
                    }
                })
        );
    }

    /** 手写识别 */
    public void handwriting(String url, final Callback mCallback){

        mCompositeSubscription.add(

                mHttpGo.fileSubscription("xfyun/handwriting", new File(url), new HttpGo.HttpGoCallback() {
                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onNext(HttpDataBean httpDataBean) {

                        if (httpDataBean.getCode()==0){

                            if (httpDataBean.getData()!=null)mCallback.getData(httpDataBean.getData());
                        }
                    }
                })
        );
    }

    /** 保存文档 */
    public void addwork(Map<String,String> httpdata, final Callback mCallback){

        mCompositeSubscription.add(

                mHttpGo.postSubscription("user_works/add", httpdata, new HttpGo.HttpGoCallback() {
                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onNext(HttpDataBean httpDataBean) {

                        if (httpDataBean.getCode()==0)mCallback.getData(httpDataBean.getData());
                    }
                })
        );
    }

    /** 导出文档 */
    public void export(Map<String,String> httpdata, final Callback mCallback){

        mCompositeSubscription.add(

                mHttpGo.postSubscription("user_works/export", httpdata, null,new HttpGo.HttpGoCallback() {
                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onNext(HttpDataBean httpDataBean) {

                        if (httpDataBean.getCode()==0){

                            if (httpDataBean.getData()!=null)mCallback.getData(httpDataBean.getData());
                        }
                    }
                })
        );

    }

    /** 文档详情 */
    public void get(final String id, final Callback mCallback){

        Map<String,String> map = new HashMap<>();
        map.put("id",id);

        mCompositeSubscription.add(

                mHttpGo.postSubscription("user_works/get", map, new HttpGo.HttpGoCallback() {
                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onNext(HttpDataBean httpDataBean) {

                        if (httpDataBean.getCode()==0){

                            if (httpDataBean.getData()!=null)mCallback.getData(httpDataBean.getData());
                        }
                    }
                })
        );
    }

}
